package com.lenovo.manufacture;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class TalentMarket extends AppCompatActivity {
private Button btReturn;
private ImageButton iBtype;
private ImageButton ibMoney;
private ListView listView;
Mydapter myAdapter;
int k=0;
int j=0;
private List<PeopleData.DataBean> data=new ArrayList<>();
private final Retrofit retrofit=new Retrofit.Builder()
        .baseUrl("http://hh1.me:5001/Interface/")
        .addConverterFactory(GsonConverterFactory.create())
        .build();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_talentmarket);
        initView();
        getPeolple();
    }

    private void getPeolple(){
        Service service=retrofit.create(Service.class);
        Call<PeopleData> ispeople=service.isPeople();
        ispeople.enqueue(new Callback<PeopleData>() {
            @Override
            public void onResponse(Call<PeopleData> call, Response<PeopleData> response) {
                data.addAll(response.body().getData());
                myAdapter=new Mydapter();
                listView.setAdapter(myAdapter);
                setEvent();
            }

            @Override
            public void onFailure(Call<PeopleData> call, Throwable t) {

            }
        });
    }

    private void setEvent(){
        iBtype.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (k==0){
                    iBtype.setImageResource(R.mipmap.up);
                    Collections.sort(data, new Comparator<PeopleData.DataBean>() {
                        @Override
                        public int compare(PeopleData.DataBean o1, PeopleData.DataBean o2) {
                            int i=o1.getType()-o2.getType();
                            return i;
                        }
                    });
                    k=1;
                    myAdapter.notifyDataSetChanged();
                }else {
                    iBtype.setImageResource(R.mipmap.down);
                    Collections.sort(data, new Comparator<PeopleData.DataBean>() {
                        @Override
                        public int compare(PeopleData.DataBean o1, PeopleData.DataBean o2) {
                            int i=o2.getType()-o1.getType();
                            return i;
                        }
                    });
                    k=0;
                    myAdapter.notifyDataSetChanged();
                }
            }
        });
        ibMoney.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (j==0){
                    ibMoney.setImageResource(R.mipmap.up);
                    Collections.sort(data, new Comparator<PeopleData.DataBean>() {
                        @Override
                        public int compare(PeopleData.DataBean o1, PeopleData.DataBean o2) {
                            int i=o1.getPrice()-o2.getPrice();
                            return i;
                        }
                    });
                    j=1;
                    myAdapter.notifyDataSetChanged();
                }else {
                    ibMoney.setImageResource(R.mipmap.down);
                    Collections.sort(data, new Comparator<PeopleData.DataBean>() {
                        @Override
                        public int compare(PeopleData.DataBean o1, PeopleData.DataBean o2) {
                            int i=o2.getPrice()-o1.getPrice();
                            return i;
                        }
                    });
                    j=0;
                    myAdapter.notifyDataSetChanged();
                }
            }
        });
        btReturn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
    private void initView() {
        btReturn=findViewById(R.id.bt_return);
        iBtype =findViewById(R.id.ib_type);
        ibMoney =findViewById(R.id.ib_money);
        listView=findViewById(R.id.lv_people);
    }

    private class Mydapter extends BaseAdapter{
        private TextView nameText;
        private TextView typeText;
        private TextView moneyText;

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView= LayoutInflater.from(TalentMarket.this).inflate(R.layout.people_row,parent,false);
            nameText=convertView.findViewById(R.id.name_tv);
            typeText=convertView.findViewById(R.id.type_tv);
            moneyText=convertView.findViewById(R.id.money_tv);
            nameText.setText(data.get(position).getPersonName());
            if (data.get(position).getType()==0){
                typeText.setText("工程师");
            }else if (data.get(position).getType()==1){
                typeText.setText("工人");
            }else if (data.get(position).getType()==2){
                typeText.setText("技术人员");
            }else{
                typeText.setText("质检员");
            }
            moneyText.setText(String.valueOf(data.get(position).getPrice()));
            return convertView;
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }
}
